<?php
/**
 * 搜索结果导出
 * User: dxk
 * Date: 2020/07/17
 */

namespace App\Library\Service;

class ExportService
{
    /**
     * 后台异步导出时弹窗
     * @param $export_service //导出服务
     * @param array $allow_export_fields //允许导出字段
     * @param array $default_export_fields //默认选中导出字段
     * @param string $attribute 导出节点clss名称
     * @param string $return_url 跳转地址
     * @return string
     */
    public static function coreBootstrapTableExport($export_service, $allow_export_fields = [], $default_export_fields = [], $attribute = 'bootstrap_table_export', $return_url = '')
    {
        $export_service = base64_encode($export_service);
        $export_url = easy_url('core/help/file/export');
        if (empty($return_url)) {
            $return_url = easy_url('core/help/file');
        }
        $return_url = base64_encode($return_url);
        $attribute = trim($attribute);
        if (empty($attribute)) {
            $attribute = 'bootstrap_table_export';
        }
        if (empty($default_export_fields)) {
            $default_export_fields = array_keys($allow_export_fields);
        }
        $js = '$(' . "'." . $attribute . "'" . ').click(function () { console.log(table_response);';
        $js .= 'if(table_response["total"] <=0 ){ layer.msg("没有数据");return false;}';
        $content_str = "<div id='export_div' style='padding: 15px;'>";
        if (is_array($allow_export_fields) && !empty($allow_export_fields)) {
            $content_str .= "<div class='checkbox checkbox-primary'><input id='export_chkAll' class='i-checks export_chkAll' type='checkbox' name='checkAll'><label for='export_chkAll'>全选</label></div>";
            foreach ($allow_export_fields as $key => $item) {
                $content_str .= "<div class='checkbox  checkbox-primary col-sm-3'><input type='checkbox' id='check_$key' value='$key' name='export_fields[]' class='form-control export_fields' ";
                if (is_array($default_export_fields) && in_array($key, $default_export_fields)) {
                    $content_str .= "checked='checked'";
                }
                $content_str .= "><label for='check_$key'>$item</label></div>";
            }
        }
        $content_str .= "<div class='form-group col-sm-10'><input type='text' id='remark' class='form-control' maxlength='20' placeholder='请填写备注'></div>";
        $content_str .= '</div>';
        $width = '80%';
        $height = '80%';
        if (empty($allow_export_fields)) {
            $width = '80%';
            $height = '50%';
        }

        $js .= "
        let export_url = '{$export_url}' + '?' + $.param(table_params);
         layer.open({
                type: 1,
                title: '导出备注',
                shadeClose: true,
                skin: 'layui-layer-rim', //加上边框
                area: ['{$width}', '{$height}'], //宽高
                content: \"{$content_str}\",
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var remark = $.trim($('#remark').val());
                    if (remark == '') {
                        layer.msg('请填写备注');
                        return false;
                    }
                    var ddd = [];
                    $('#export_div').find('.export_fields:checked').each(function () {
                         ddd.push($(this).val());
                    });
                    var index1 = layer.load(1, {
                        shade: [0.1, '#fff']
                    });
                    $.post(export_url, {
                        remark: remark,
                        total:table_response[\"total\"],
                        service: \"{$export_service}\",
                        return_url: \"{$return_url}\",
                        export_fields: ddd
                    }, function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 2});
                            layer.close(index1);
                            return false;
                        } else {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);
                            layer.close(index1);
                            location.href = res.data.jump_url;
                        }
                    }, 'json');
                },
                no: function (index, layero) {
                    layer.close(index);
                    layer.close(index1);
                }
            });
        ";

        $js .= "
        $('.export_chkAll').on('change', function () {
            var _this = $(this);
            if (_this.is(':checked')) {
                $('.export_fields').prop('checked', true);
            } else {
                $('.export_fields').prop('checked', false);
            }
        });
        ";
        return self::JS($js);
    }

    private static function JS($js)
    {
        return $js . ' return false; });';
    }

    /**
     * 加密路径
     * @param $file_path
     * @return string
     */
    public static function encryptFilePath($file_path)
    {
        return \Crypt::encryptString($file_path);
    }

    /**
     * 解密路径
     * @param $file_path
     * @return string
     */
    public static function decryptFilePath($file_path)
    {
        return \Crypt::decryptString($file_path);
    }

    /**
     * @param $count
     * @param $export_service
     * @param $params
     * @param $allow_export_fields
     * @param $default_export_fields
     * @param $attribute
     * @param $return_url
     * @return string
     */
    public static function asyncExport($count, $export_service, $params, $allow_export_fields = [], $default_export_fields = [], $attribute = 'async_export', $return_url = '')
    {
        $export_service = base64_encode($export_service);
        $export_url = easy_merchant_url('help/file/export');
        if (empty($return_url)) {
            $return_url = easy_merchant_url('help/file');
        }
        $return_url = base64_encode($return_url);
        $attribute = trim($attribute);
        if (empty($attribute)) {
            $attribute = 'async_export';
        }
        if (empty($default_export_fields)) {
            $default_export_fields = array_keys($allow_export_fields);
        }
        $js = '$(' . "'." . $attribute . "'" . ').click(function () {';
        if (!empty($params)) {
            $js .= 'var params = ' . json_encode($params);
        }
        if ($count <= 0) {
            $js .= 'layer.msg("没有数据");';
            return self::JS($js);
        }
        $content_str = "<div id='export_div' style='padding: 15px;'>";
        if (is_array($allow_export_fields) && !empty($allow_export_fields)) {
            $content_str .= "<div class='checkbox checkbox-primary'><input id='export_chkAll' class='i-checks export_chkAll' type='checkbox' name='checkAll'><label for='export_chkAll'>全选</label></div>";
            foreach ($allow_export_fields as $key => $item) {
                $content_str .= "<div class='checkbox  checkbox-primary col-sm-3'><input type='checkbox' id='check_$key' value='$key' name='export_fields[]' class='form-control export_fields' ";
                if (is_array($default_export_fields) && in_array($key, $default_export_fields)) {
                    $content_str .= "checked='checked'";
                }
                $content_str .= "><label for='check_$key'>$item</label></div>";
            }
        }
        $content_str .= "<div class='form-group col-sm-10'><input type='text' id='remark' class='form-control' maxlength='20' placeholder='请填写备注'></div>";
        $content_str .= '</div>';
        $width = '800px';
        $height = '400px';
        if (empty($allow_export_fields)) {
            $width = '400px';
            $height = '180px';
        }

        $js .= "
         layer.open({
                type: 1,
                title: '导出备注',
                shadeClose: true,
                skin: 'layui-layer-rim', //加上边框
                area: ['{$width}', '{$height}'], //宽高
                content: \"{$content_str}\",
                btn: ['确定', '取消'],
                yes: function (index, layero) {
                    var remark = $.trim($('#remark').val());
                    if (remark == '') {
                        layer.msg('请填写备注');
                        return false;
                    }
                    var ddd = [];
                    $('#export_div').find('.export_fields:checked').each(function () {
                         ddd.push($(this).val());
                    });
                    var index1 = layer.load(1, {
                        shade: [0.1, '#fff']
                    });

                    var postData = {
                        export_remark: remark,
                        total:\"{$count}\",
                        service: \"{$export_service}\",
                        return_url: \"{$return_url}\",
                        export_fields: ddd
                    }

                    $.each(params,function(i,v){
                        postData[i] = v;
                    });
                    console.log('params',params)
                    $.post(\"{$export_url}\", postData, function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 2});
                            layer.close(index1);
                            return false;
                        } else {
                            layer.msg(res.msg, {icon: 1});
                            layer.close(index);
                            layer.close(index1);
                            location.href = res.data.jump_url;
                        }
                    }, 'json');
                },
                no: function (index, layero) {
                    layer . close(index);
                    layer . close(index1);
                }
            });
        ";

        $js .= "
        $('.export_chkAll').on('change', function () {
            var _this = $(this);
            if (_this.is(':checked')) {
                $('.export_fields').prop('checked', true);
            } else {
                $('.export_fields').prop('checked', false);
            }
        });
        ";
        return self::JS($js);
    }
}
